home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / strlist3 / strlist.txt < prev   
Text File  |  1996-02-26  |  47KB  |  1,413 lines

  1. file strlist.txt Copyright (C) 1995, 1996 by John Montbriand.  All Rights Reserved.
  2.  
  3. ABOUT STRLIST...
  4.  
  5.     Copyright (C) 1995, 1996 by John Montbriand.  All Rights Reserved.
  6.     
  7.     Distribute freely in areas where the laws of copyright apply.
  8.  
  9.     Use at your own risk.
  10.     
  11.     Do not distribute modified copies.
  12.  
  13.     These various string list libraries are for free!
  14.  
  15.  
  16. ABOUT THE AUTHOR...
  17.  
  18.     Comments, questions, or suggestions are always welcome.
  19.  
  20.     John Montbriand                tinyjohn@sasknet.sk.ca
  21.     P.O. Box. 1133
  22.     Saskatoon Saskatchewan Canada
  23.     S7K 3N2
  24.  
  25.  
  26. WHY IS IT FOR FREE?
  27.  
  28.     none of your business,
  29.     
  30.     but if you have some business for me,  I'll see what I can do..
  31.  
  32.  
  33. OUTLINE OF TOPICS
  34.  
  35.     STRING LISTS, WHAT ARE THEY?
  36.     IMPLEMENTATION NOTES
  37.     MATERIALS
  38.     LEGAL STUFF
  39.     FURTHER REFERENCE
  40.     SERVICES PROVIDED (OVERVIEW)
  41.     BE CAREFUL (CAVEATS)
  42.     COMPARISONS AND ORDERING
  43.     OVERVIEW OF ROUTINES
  44.     SPECIFIC ROUTINE DESCRIPTIONS
  45.  
  46.  
  47. STRING LISTS, WHAT ARE THEY?
  48.     
  49.     Strings are commonly used to store textual information for retrieval and
  50.     display for human interpretation.  On the macintosh, the STR# resource
  51.     type has been around since the beginning of mactime and in itself
  52.     it can be particularily useful when dealing with strings and groups
  53.     of strings, while at the same time it is an ecconomical way to store
  54.     strings in memory and on disk.
  55.  
  56.     String lists are a storage efficient way to store the string type data.
  57.  
  58.  
  59. IMPLEMENTATION NOTES
  60.  
  61.     String lists are stored as A two byte integer value containing
  62.     the number of strings that follow.  Each string is stored
  63.     consecutively end to end as a length byte followed by the data
  64.     bytes making up the string.
  65.  
  66.     Elements in string lists are indexed as 1, 2, ..., n and all of the
  67.     routines defined herein use this convention.
  68.  
  69.     As string lists are stored in handles, there are other non-obvious
  70.     issues involved here.  Most notably, some of the routines herein
  71.     return pointers to strings stored inside of string lists (which
  72.     are stored in side of handles).  Handles can be moved by other
  73.     routines that call the memory manager and as such, this behavior
  74.     could invalidate such a string pointer unless the string list handle
  75.     is locked.
  76.     
  77.     THE RULE:  when you use the routine StringListElt to retrieve strings
  78.     from string lists, use HLock to lock the string list.
  79.  
  80.  
  81. MATERIALS
  82.  
  83.     The STR# format itself is the copyright property of Apple computer
  84.     and has been known to the public since 1983.
  85.     
  86.     The routines documented herein are the copyright property of
  87.     John Montbriand.  I am making these routines available to the
  88.     macintosh programming community because I feel it's about time
  89.     someone did!  I hope all who find them, use them well.
  90.  
  91.     Compiled and tested with MPW C, MW C, Symantic C, and MPW PPC.
  92.  
  93.  
  94. LEGAL STUFF
  95.  
  96.     These libraries are provided for free and you may use them
  97.     in any program you make;  however, if you're particular about
  98.     having signed documentation that proves this fact in your posession
  99.     then, as I would like to ensure these libraries are widely used,
  100.     I would be glad provide such documents for you.
  101.     
  102.     I don't think this is entirly necessary, but I can understand
  103.     that some folks feel quite strongly about such issues and prefer
  104.     to do things in writing.
  105.     
  106.     If you would like for me to send you a signed document saying
  107.     that you have license to use these routines in your programs,
  108.     then send me a couple of bucks to cover postage and handling
  109.     and I'll send you back a signed letter saying you have license
  110.     to use these routines in any software you make.
  111.     
  112.     I think two or three dollars is fair as I don't really get a big
  113.     thrill out of printing stuff out and mailing it, and that's usually
  114.     enough to cover postage to anywhere in the world.  I s'pose you'd have
  115.     to figure out how much it would cost for me to mail a letter to you
  116.     from here in Canada.
  117.  
  118.  
  119. FURTHER REFERENCE
  120.  
  121.     strlist.h and strlist.c contain routines for accessing the string
  122.     list data type.  You can use these routines to access or modify
  123.     information stored in any STR# resource.  As well, you can use
  124.     these routines for creating and manipulating lists of strings
  125.     in memory.  The following sources describe what is involved here
  126.     in more detail:
  127.     
  128.     Inside Macintosh: Overview by Apple Computer, Inc. Addison-Wesley.
  129.         on Page 52 there is some discussion of the STR# resource format
  130.     and the routine GetIndString.
  131.    
  132.     Inside Macintosh: Text by Apple Computer, Inc. Addison-Wesley.
  133.         on Page 5-50 GetIndString function is described, page 5-9
  134.     includes some discussion of the string list format.
  135.  
  136.     Inside Macintosh: Memory by Apple Computer, Inc.  Addison-Wesley.
  137.         A discussion of memory managment, handles, what they are, and
  138.         how to use them.
  139.     
  140.     The C Programming Language 2nd edition by Brian W. Kernighan
  141.         and Dennis M. Ritchie.  Prentice Hall.
  142.     
  143.     Comments for the routines provided herein have been included in the
  144.     file strlist.h.
  145.     
  146.     Also, there is a 411 help file.
  147.  
  148.  
  149. SERVICES PROVIDED (OVERVIEW)
  150.  
  151.     - routines for creating string lists in memory or retrieving
  152.     them from resource files.
  153.     
  154.     - routines for querying information stored in string lists
  155.     including their size, and in-place access to strings in the list.
  156.     
  157.     - routines for creating mapped index vectors containing pointers
  158.     to strings stored in string lists.  string list maps provide
  159.     quick access to strings stored in string lists.  both sorted maps
  160.     and unsorted maps can be created.
  161.     
  162.     - a complete set of operations for adding, deleting, and inserting,
  163.     strings into string lists, including routines for adding strings
  164.     to alphabetically sorted string lists and facilities for searching
  165.     string lists.
  166.  
  167.     - routines for building menus from string lists, and routines for
  168.     filling list manager lists using strings stored in string lists.
  169.  
  170.     - routines for treating string lists as 'sets' of strings providing
  171.     the traditional AND, OR, and XOR set operations, as well as subset
  172.     and equivalence tests.  
  173.  
  174.  
  175. BE CAREFUL (CAVEATS)
  176.  
  177.     I think these routines are safe to use, but you should do your own
  178.     testing.  I have provided the source code, and if you find any bugs,
  179.     please let me know right away so I can provide corrections in
  180.     future versions.
  181.     
  182.     I advertise a finder's fee of $10.00 cash for errors in any
  183.     of my products.
  184.  
  185.  
  186. COMPARISONS AND ORDERING
  187.  
  188.     All string comparisons are done using the OSUtilities function RelString.
  189.     the compile time variable SLUSECASE determines if the comparison will
  190.     be case sensitive or non case sensitive.  by default, SLUSECASE is defined
  191.     as false.  you can override this by predefining the compile time variable
  192.     before compiling strlist.c
  193.     
  194.     All of the routines that treat string lists as sets of strings expect
  195.     that the string lists passed to them will be alphabetically sorted.
  196.  
  197.  
  198. OVERVIEW OF ROUTINES
  199.  
  200.     creating your own string lists:
  201.         Handle NewStringList(void);
  202.         Handle MakeStringList(long n, ...);
  203.     
  204.     retrieving string lists stored in resource files:
  205.         Handle GetStringList(short id);
  206.         Handle Get1StringList(short id);
  207.     
  208.     accessing information in string lists:
  209.         short StringListSize(Handle list);
  210.         StringPtr StringListElt(Handle list, short elt); /* O(n) */
  211.         StringPtr RetrieveIndString(Handle list, short elt, StringPtr the_string);
  212.    
  213.     recovering memory occupied by string lists:
  214.         void DisposeStringList(Handle list);
  215.  
  216.     speedy access routines for information in string lists via a map:
  217.         StringPtr** MakeStringListMap(Handle list);
  218.         StringPtr MapStringListElt(StringPtr** string_map, short elt);/*O(k)*/
  219.         WithStringList(stringlist, map)  /* MACRO */
  220.         
  221.     speedy access routines for information in string lists via sorted map:
  222.         StringPtr** MakeSortedStringListMap(Handle list);
  223.         WithSortedStringList(stringlist, map)  /* MACRO */
  224.     
  225.     deleting elments from a string list:
  226.         void StringListRemove(Handle list, short elt);
  227.         void ClearStringList(Handle list);
  228.  
  229.     adding elements to a string list (unsorted):
  230.         void StringListInstall(Handle list, short elt, StringPtr s);
  231.         void StringListAppend(Handle list, StringPtr s);
  232.         void StringListPrepend(Handle list, StringPtr s);
  233.     
  234.     adding elements to a string list (sorted):
  235.         short StringListInsert(Handle list, StringPtr s);
  236.         short StringListRInsert(Handle list, StringPtr s);
  237.     
  238.     finding elements in a string list (test for membership in sets):
  239.         short FindStringList(Handle list, StringPtr s);
  240.     
  241.     creating menus using strings stored in a string list:
  242.         MenuHandle StringListToMenu(Handle list, short id, StringPtr name);
  243.     
  244.     filling list manager lists using strings stored in a string list:
  245.         void StringListToList(Handle list, ListHandle the_list);
  246.  
  247.     using string lists as sets of strings:
  248.         Handle StringListUnion(Handle A, Handle B);         /*  A | B */
  249.         Handle StringListIntersection(Handle A, Handle B);  /*  A & B */
  250.         Handle StringListDifference(Handle A, Handle B);    /*  A ^ B */
  251.         Boolean StringListSubset(Handle A, Handle B);       /*  A contains B */
  252.         Boolean StringListEquivalent(Handle A, Handle B);   /*  A == B */
  253.  
  254.  
  255. SPECIFIC ROUTINE DESCRIPTIONS
  256.  
  257.  
  258. NewStringList
  259.  
  260.     formal declaration:
  261.         Handle NewStringList(void);
  262.     
  263.     arguments:
  264.         none
  265.     
  266.     return value:
  267.         A Handle containing a empty string list. 
  268.         if an error occurs, NULL is returned.
  269.         
  270.     description:
  271.         NewStringList creates a new string list handle in the current
  272.         heap zone.  The string list created contains no strings, and the
  273.         count value is set to zero.
  274.  
  275.     example application:
  276.         In this example, we create a new, empty string list:
  277.     
  278.         {   Handle my_string_list;
  279.             
  280.             my_string_list = NewStringList();
  281.             ...
  282.     notes:
  283.         you can call DisposeHandle to dispose of a string list created by
  284.         NewStringList.
  285.  
  286.  
  287.  
  288. MakeStringList
  289.  
  290.     formal declaration:
  291.         Handle MakeStringList(long n, ...);
  292.     
  293.     arguments:
  294.         n = the number of c style strings that follow in the ... parameter list
  295.         ... = n c style strings
  296.     
  297.     return value:
  298.         A Handle to a string list containing the strings specified in the ...
  299.         parameter list in the same order as they were provided as parameters. 
  300.         if an error occurs, NULL is returned.
  301.             
  302.     description:
  303.         MakeStringList creates a new string list handle in the current
  304.         heap zone containing the strings provided as parameters.  the
  305.         number of strings provided as parameters must match the number
  306.         of specified in the value n.  if an error occurs, NULL is returned.
  307.  
  308.     example application:
  309.         in this example we create a string list containing 5 elements:
  310.     
  311.         {   Handle my_string_list;
  312.             
  313.             my_string_list = MakeStringList(5, "this", "is", "a", "string",
  314.                  "list");
  315.             ...
  316.     notes:
  317.         you can call DisposeHandle to dispose of a string list created by
  318.         MakeStringList.
  319.  
  320.  
  321.  
  322. GetStringList
  323.  
  324.     formal declaration:
  325.         Handle GetStringList(short id);
  326.     
  327.     arguments:
  328.         id = the resource id of the STR# resource containing the string list
  329.     
  330.     return value:
  331.         A Handle to a string list resource or NULL if an error occurs.
  332.             
  333.     description:
  334.         GetStringList is equivalent to GetResource('STR#', id).
  335.  
  336.     example application:
  337.         in this example, we retrieve the string list with id 128:
  338.     
  339.         {   Handle my_string_list;
  340.             
  341.             my_string_list = GetStringList(128);
  342.             ...
  343.     notes:
  344.         you should not call DisposeHandle to dispose of a string list
  345.         retrieved by GetStringList as such string lists are registed
  346.         in the resource file.  Instead, after you are done with the string
  347.         list call ReleaseResource(<the string list>).
  348.         You can call DisposeStringList on a string list created by
  349.         GetStringList.
  350.  
  351.  
  352.  
  353. Get1StringList
  354.  
  355.     formal declaration:
  356.         Handle Get1StringList(short id);
  357.     
  358.     arguments:
  359.         id = the resource id of the STR# resource containing the string list
  360.     
  361.     return value:
  362.         A Handle to a string list resource or NULL if an error occurs.
  363.             
  364.     description:
  365.         Get1StringList is equivalent to Get1Resource('STR#', id) and hence
  366.         it only searches the current resource file for string list resources.
  367.  
  368.     example application:
  369.         in this example, we retrieve the string list with id 128
  370.     searching only the current resource file:
  371.     
  372.         {   Handle my_string_list;
  373.             
  374.             my_string_list = GetStringList(128);
  375.             
  376.             ...
  377.     notes:
  378.         you should not call DisposeHandle to dispose of a string list
  379.         retrieved by Get1StringList as such string lists are registed
  380.         in the resource file.  Instead, after you are done with the string
  381.         list call ReleaseResource(<the string list>).
  382.         You can call DisposeStringList on a string list created by
  383.         GetStringList.
  384.  
  385.  
  386.  
  387. DisposeStringList
  388.  
  389.     formal declaration:
  390.         void DisposeStringList(Handle list);
  391.     
  392.     arguments:
  393.         list = a handle to a string list created by NewStringList,
  394.             MakeStringList, GetStringList, or Get1StringList.
  395.     
  396.     return value:
  397.         none.
  398.             
  399.     description:
  400.         DisposeStringList recovers the memory occupied by a string list.
  401.         After determining if the handle refers to a resource by checking
  402.         the handle's flag values, DisposeStringList either calls
  403.         ReleaseResource for resource handles or calls DisposeHandle
  404.         for regular handles.
  405.  
  406.     example application:
  407.         Here, we a get a string list from a resource file, and
  408.         and do away with it, then we create a new string list and
  409.         do away with it as well.  Note:  DisposeStringList does
  410.         the right thing in both cases calling ReleaseResource for
  411.         the resource handle and calling DisposeHandle for the list
  412.         we allocated in memory ourselves.
  413.     
  414.         {   Handle my_string_list;
  415.             
  416.             my_string_list = GetStringList(128);
  417.             if (my_string_list != NULL) {
  418.                 
  419.                 ...some statements using the string list...
  420.             
  421.                 DisposeStringList(my_string_list);
  422.             }
  423.             
  424.             my_string_list = MakeStringList(5, "this", "is", "a",
  425.                   "string", "list");
  426.             if (my_string_list != NULL) {
  427.                 
  428.                 ...some statements using the string list...
  429.                 
  430.                 DisposeStringList(my_string_list);
  431.             }
  432.             ...
  433.     notes:
  434.         none.
  435.  
  436.  
  437.  
  438. StringListSize
  439.  
  440.     formal declaration:
  441.         short StringListSize(Handle list);
  442.     
  443.     arguments:
  444.         list = a handle to a string list created by NewStringList,
  445.             MakeStringList, GetStringList, or Get1StringList.
  446.     
  447.     return value:
  448.         the number of strings contained in the string list parameter.
  449.             
  450.     description:
  451.         StringListSize returns the value stored in the count field of the
  452.         string list handle.  the number returned corresponds to the number
  453.         of strings stored in the list.
  454.  
  455.     example application:
  456.         here we get a string list resource and calculate it's size.
  457.     
  458.         {   Handle my_string_list;
  459.             short number_of_strings;
  460.             
  461.             my_string_list = GetStringList(128);
  462.             
  463.             number_of_strings = StringListSize(my_string_list);
  464.             ...
  465.     notes:
  466.         none.
  467.  
  468.  
  469.  
  470. StringListElt
  471.  
  472.     formal declaration:
  473.         StringPtr StringListElt(Handle list, short elt);
  474.     
  475.     arguments:
  476.         list = a handle to a string list created by NewStringList,
  477.             MakeStringList, GetStringList, or Get1StringList.
  478.         elt = the index of the string list element.  remember, elements
  479.             in string lists are indexed 1, 2, 3, ..., n.
  480.     
  481.     return value:
  482.         a pointer to the requested string list element or NULL if elt.
  483.             
  484.     description:
  485.         StringListElt returns a pointer to a specific element in the string
  486.         list handle.  The pointer returned refers directly to the data inside
  487.         of the string list handle.
  488.  
  489.     example application:
  490.         Here, we draw every element in a string list.  Note the calls
  491.         to HLock and HUnlock surrounding the part where we access the
  492.         strings.
  493.     
  494.         {   Handle my_string_list;
  495.             short number_of_strings, index;
  496.             StringPtr a_string;
  497.             
  498.             my_string_list = GetStringList(128);
  499.             
  500.             number_of_strings = StringListSize(my_string_list);
  501.             
  502.             HLock(my_string_list); /* lock the handle so it does not move */
  503.             
  504.             for (index = 1; index <=  number_of_strings; index++) {
  505.                 
  506.                 a_string = StringListElt(my_string_list, index);
  507.                 
  508.                 MoveTo(10, index*20);
  509.                 DrawString(a_string);
  510.             }
  511.             
  512.             HUnlock(my_string_list);
  513.             ...
  514.     notes:
  515.         StringListElt returns a pointer that refers to data stored inside
  516.         of the string list handle.  Some toolbox calls can cause handles
  517.         to move in memory and invalidate this pointer unless the string
  518.         list handle is locked. Also, the pointer returned may not always
  519.         be alligned at an even address.
  520.         
  521.         Each call to StringListElt involves a linear search of the string
  522.         list for the element requested.  If you are frequently accessing
  523.         strings stored in a string list and you are noticing a performance
  524.         dropoff because of this, then you should call MakeStringListMap
  525.         and access the strings directly using the MapStringListElt routine
  526.         (StringListElt is uses O(n) time per lookup while MapStringListElt
  527.         uses O(k) time per lookup).  For example, the example application
  528.         in StringListElt requires O(n*n) time while the example application
  529.         of MakeStringListMap only requires O(n) time for the same task, and
  530.         MakeSortedStringListMap only requires O(nlogn) time.
  531.  
  532.  
  533.  
  534. RetrieveIndString
  535.  
  536.     formal declaration:
  537.         StringPtr RetrieveIndString(Handle list, short elt, StringPtr the_string);
  538.     
  539.     arguments:
  540.         list = a handle to a string list created by NewStringList,
  541.             MakeStringList, GetStringList, or Get1StringList.
  542.         elt = the index of the string list element.  remember, elements
  543.             in string lists are indexed 1, 2, 3, ..., n.
  544.     the_string = a pointer to a variable of type Str255 to copy
  545.         the string into.
  546.         
  547.     return value:
  548.         the_string, or NULL if the elt was out of bounds.
  549.             
  550.     description:
  551.         RetrieveIndString copies the indicated string list element from
  552.         the string list into the string pointed to by the_string.
  553.         RetrieveIndString returns the value passed in the third
  554.         parameter, or NULL if the index was out of bounds.
  555.  
  556.     example application:
  557.         here, we copy element 4 from the string list into the
  558.         string variable my_string:
  559.     
  560.         {    Handle my_string_list;
  561.              Str255 my_string;
  562.          
  563.              my_string_list = GetStringList(128);
  564.              RetrieveIndString(my_string_list, 4, my_string);
  565.              ...
  566.         
  567.     notes:
  568.         you do not have to lock the string list when using this
  569.         call to retrieve string list elements.
  570.  
  571.  
  572.  
  573. MakeStringListMap
  574.  
  575.     formal declaration:
  576.         StringPtr** MakeStringListMap(Handle list);
  577.     
  578.     arguments:
  579.         list = a handle to a string list created by NewStringList,
  580.             MakeStringList, GetStringList, or Get1StringList.
  581.         
  582.     return value:
  583.         a handle to an array of string pointers.
  584.             
  585.     description:
  586.         MakeStringListMap creates an array of string pointers referring to
  587.         consecutive string list elements.  The string list is locked high in
  588.         the heap before the mapping array is created.  A map created in this
  589.         way can be used to directly access string list elements using the
  590.         function MapStringListElt without the linear search overhead required
  591.         by StringListElt.
  592.  
  593.     example application:
  594.         in this example, we use a string list map to consecutively
  595.         draw string list elements on the screen.
  596.         {   Handle my_string_list;
  597.             StringPtr** the_map, the_string;
  598.             short number_of_strings, i;
  599.             
  600.             my_string_list = NewStringList();
  601.             
  602.             ...several hundred strings are added here...
  603.             
  604.             the_map = MakeStringListMap(my_string_list);
  605.             if (the_map != NULL) {
  606.                 number_of_strings = StringListSize(my_string_list);
  607.     
  608.                 for (i=1; i <= number_of_strings; i++) {
  609.                     
  610.                     the_string = MapStringListElt(the_map, i);
  611.                     
  612.                     MoveTo(10, i*10);
  613.                     DrawString(the_string);
  614.                 }
  615.                 HUnlock(my_string_list);
  616.                 DisposeHandle((Handle) the_map);
  617.             }
  618.             ...
  619.     notes:
  620.         a map created by MakeStringListMap can be disposed of with
  621.         a call to DisposeHandle((Handle) the_map).
  622.         Since MakeStringListMap locks the string list handle, you
  623.         should remember to unlock the handle once you dispose of the map.
  624.     also note:
  625.         do not unlock the string list handle until you are completely
  626.         done with the map--unlocking the string list will invalidate
  627.         pointers stored in the map.
  628.         
  629.  
  630.  
  631. MakeSortedStringListMap
  632.  
  633.     formal declaration:
  634.         StringPtr** MakeSortedStringListMap(Handle list);
  635.     
  636.     arguments:
  637.         list = a handle to a string list created by NewStringList,
  638.             MakeStringList, GetStringList, or Get1StringList.
  639.         
  640.     return value:
  641.         a handle to an array of string pointers with the pointers sorted
  642.         so that the map refers to the strings in the string list in
  643.         alphabetical order.  if SLUSECASE is true, case sensitive ordering
  644.         is used.
  645.             
  646.     description:
  647.         MakeSortedStringListMap is identical to MakeStringListMap except
  648.         that the pointers in the map are sorted  so that they refer
  649.         to the string in the string list in aphabetical order.  
  650.  
  651.     example application:
  652.         in this example, we use a string list map to consecutively
  653.         draw string list elements on the screen in alphabetical order.
  654.         {   Handle my_string_list;
  655.             StringPtr** the_map, the_string;
  656.             short number_of_strings, i;
  657.             
  658.             my_string_list = NewStringList();
  659.             
  660.             ...several hundred strings are added here...
  661.             
  662.             the_map = MakeSortedStringListMap(my_string_list);
  663.             if (the_map != NULL) {
  664.                 number_of_strings = StringListSize(my_string_list);
  665.     
  666.                 for (i=1; i <= number_of_strings; i++) {
  667.                     
  668.                     the_string = MapStringListElt(the_map, i);
  669.                     
  670.                     MoveTo(10, i*10);
  671.                     DrawString(the_string);
  672.                 }
  673.                 HUnlock(my_string_list);
  674.                 DisposeHandle((Handle) the_map);
  675.             }
  676.             ...
  677.     notes:
  678.         a map created by MakeSortedStringListMap can be disposed of with
  679.         a call to DisposeHandle((Handle) the_map).
  680.         Since MakeSortedStringListMap locks the string list handle, you
  681.         should remember to unlock the handle once you dispose of the map.
  682.     also note:
  683.         do not unlock the string list handle until you are completely
  684.         done with the map--unlocking the string list will invalidate
  685.         pointers stored in the map.
  686.         
  687.  
  688.  
  689. MapStringListElt
  690.  
  691.     formal declaration:
  692.         StringPtr MapStringListElt(StringPtr** string_map, short elt);
  693.     
  694.     arguments:
  695.         string_map = a string list map created by MakeStringListMap.
  696.         elt = the index of the string list element.  remember, elements in
  697.             string lists are indexed 1, 2, 3, ..., n.
  698.         
  699.     return value:
  700.         a pointer to the string in the string list.
  701.             
  702.     description:
  703.         MapStringListElt uses the string list map to look up the address of the
  704.         requested string in the string list.
  705.  
  706.     example application:
  707.         see MakeStringListMap
  708.     
  709.     notes:
  710.         none.
  711.  
  712.  
  713.  
  714. WithStringList MACRO
  715.  
  716.     formal declaration:
  717.         #define WithStringList(stringlist, map)
  718.  
  719.     arguments:
  720.         stringlist = an value of type string list.
  721.         map = a variable of type StringPtr** for use in the macro.
  722.             you do not need to initialize this variable, all you do is
  723.             declare it.
  724.  
  725.     return value:
  726.         not applicable.
  727.  
  728.     description:
  729.         WithStringList creates a map for the string list that will be
  730.         available for use in the statement immediately following the
  731.         macro invocation.  In addition, the macro disposes of the map and
  732.         unlocks the list once the statement has completed execution.  It is
  733.         usually most convienient to follow the macro with a compound
  734.         statement.
  735.  
  736.     example application:
  737.         here, we use the WithStringList macro to create a map then we
  738.         use the map to display the strings on the screen.
  739.     
  740.         {   Handle my_string_list;
  741.             StringPtr** the_map;
  742.             short number_of_strings, index;
  743.             
  744.             my_string_list = MakeStringList(4, "red", "green",
  745.                 "orange", "blue");
  746.             
  747.             WithStringList(my_string_list, the_map) {
  748.                 
  749.                 number_of_strings = StringListSize(my_string_list);
  750.                 for (i=1; i <= number_of_strings; i++) {
  751.                     MoveTo(10, i*10);
  752.                     DrawString(MapStringListElt(the_map, i));
  753.                 }
  754.             
  755.             }
  756.             ...            
  757.     notes:
  758.         macro parameters may be evaluated more than once in the macro
  759.         output so it's best to use variables here as in the above example.
  760.  
  761.  
  762.  
  763. WithSortedStringList MACRO
  764.  
  765.     formal declaration:
  766.         #define WithSortedStringList(stringlist, map)
  767.  
  768.     arguments:
  769.         stringlist = an value of type string list.
  770.         map = a variable of type StringPtr** for use in the macro.
  771.             you do not need to initialize this variable, all you do is
  772.             declare it.
  773.  
  774.     return value:
  775.         not applicable.
  776.  
  777.     description:
  778.         WithSortedStringList creates a map for the string list that will
  779.         be available for use in the statement immediately following the
  780.         macro invocation.  the pointers in the list are sorted so that 
  781.         the map refers to the strings in the list in alphabetical order. 
  782.         if SLUSECASE is true, case sensitive comparisons are used in 
  783.         ordering the map pointers.  In addition, the macro disposes 
  784.         of the map and unlocks the list once the statement has completed 
  785.         execution.  It is usually more convienient to follow the macro 
  786.         with a compound statement.
  787.  
  788.     example application:
  789.         Here, we use the WithSortedStringList macro to create a list of
  790.         strings, and then we use the map to display those strings on
  791.         the screen in alphabetical order.
  792.     
  793.         {   Handle my_string_list;
  794.             StringPtr** the_map;
  795.             short number_of_strings, index;
  796.             
  797.             my_string_list = MakeStringList(4, "red", "green",
  798.                 "orange", "blue");
  799.             
  800.             WithSortedStringList(my_string_list, the_map) {
  801.                 
  802.                 number_of_strings = StringListSize(my_string_list);
  803.                 for (i=1; i <= number_of_strings; i++) {
  804.                     MoveTo(10, i*10);
  805.                     DrawString(MapStringListElt(the_map, i));
  806.                 }
  807.             
  808.             }
  809.             ...            
  810.     notes:
  811.         macro parameters may be evaluated more than once in the macro
  812.         output so it's best to use variables here as in the above example.
  813.  
  814.  
  815.  
  816. StringListRemove
  817.  
  818.     formal declaration:
  819.         void StringListRemove(Handle list, short elt);
  820.     
  821.     arguments:
  822.         list = a handle to a string list created by NewStringList,
  823.             MakeStringList, GetStringList, or Get1StringList.
  824.         elt = the index of the string list element.  remember, elements
  825.             in string lists are indexed 1, 2, 3, ..., n.
  826.         
  827.     return value:
  828.         none. 
  829.             
  830.     description:
  831.         StringListRemove deletes the specified element from the string list
  832.         updating the count field appropriately.
  833.  
  834.     example application:
  835.         here, we remove element 4 from a string list.
  836.     
  837.         {   Handle my_string_list;
  838.                         
  839.             my_string_list = MakeStringList(4, "red", "green",
  840.                 "orange", "blue");
  841.             
  842.             StringListRemove(my_string_list, 3);  /* remove the word "orange" */
  843.             ...
  844.     notes:
  845.         none.
  846.  
  847.  
  848.  
  849. ClearStringList
  850.  
  851.     formal declaration:
  852.         void ClearStringList(Handle list);
  853.     
  854.     arguments:
  855.         list = a handle to a string list created by NewStringList,
  856.             MakeStringList, GetStringList, or Get1StringList.
  857.         
  858.     return value:
  859.         none. 
  860.             
  861.     description:
  862.         ClearStringList removes all of the strings from the string list
  863.         making it an empty list.
  864.  
  865.     example application:
  866.         here, we clear the string list so it contains no elements.
  867.     
  868.         {   Handle my_string_list;
  869.                         
  870.             my_string_list = MakeStringList(4, "red", "green",
  871.                 "orange", "blue");
  872.             
  873.             ClearStringList(my_string_list);
  874.             ...
  875.     notes:
  876.         none.
  877.  
  878.  
  879.  
  880. StringListInstall
  881.  
  882.     formal declaration:
  883.         void StringListInstall(Handle list, short elt, StringPtr s);
  884.     
  885.     arguments:
  886.         list = a handle to a string list created by NewStringList,
  887.             MakeStringList, GetStringList, or Get1StringList.
  888.         elt = the index where the string list element will be placed.
  889.             remember, elements in string lists are indexed 1, 2, 3, ..., n.
  890.         s = a pointer to the string to add to the list
  891.         
  892.     return value:
  893.         none. 
  894.             
  895.     description:
  896.         StringListInstall installs a string into the string list at the
  897.         indicated element position.
  898.  
  899.     example application:
  900.         Here, we insert a string into the list at position 2.
  901.     
  902.         {   Handle my_string_list;
  903.                         
  904.             my_string_list = MakeStringList(3, "the", "brown", "fox");
  905.             
  906.             /* my_string_list =  ("the", "brown", "fox") */
  907.             
  908.             StringListInstall(my_string_list, 2, "\pquick");
  909.             
  910.             /* my_string_list =  ("the", "quick", "brown", "fox") */
  911.             ...
  912.     notes:
  913.         none.
  914.  
  915.  
  916.  
  917. StringListAppend
  918.  
  919.     formal declaration:
  920.         void StringListAppend(Handle list, StringPtr s);
  921.     
  922.     arguments:
  923.         list = a handle to a string list created by NewStringList,
  924.             MakeStringList, GetStringList, or Get1StringList.
  925.         s = a pointer to the string to add to the list
  926.         
  927.     return value:
  928.         none. 
  929.             
  930.     description:
  931.         StringListAppend adds the string s to the end of the string list as
  932.         the last element in the list.
  933.  
  934.     example application:
  935.         here, we add an element to the end of a string list.
  936.     
  937.         {   Handle my_string_list;
  938.                         
  939.             my_string_list = MakeStringList(3, "my", "list", "of");
  940.             
  941.             /* my_string_list =  ("my", "list", "of") */
  942.             
  943.             StringListAppend(my_string_list, "\pstrings");
  944.             
  945.             /* my_string_list =  ("my", "list", "of", "strings") */
  946.             ...
  947.     notes:
  948.         none.
  949.  
  950.  
  951.  
  952. StringListPrepend
  953.  
  954.     formal declaration:
  955.         void StringListPrepend(Handle list, StringPtr s);
  956.     
  957.     arguments:
  958.         list = a handle to a string list created by NewStringList,
  959.             MakeStringList, GetStringList, or Get1StringList.
  960.         s = a pointer to the string to add to the list
  961.         
  962.     return value:
  963.         none. 
  964.             
  965.     description:
  966.         StringListPrepend adds the string s to the string list placing it
  967.         in the first element position.
  968.  
  969.     example application:
  970.         here, we add an element to the front of the string list.
  971.     
  972.         {   Handle my_string_list;
  973.                         
  974.             my_string_list = MakeStringList(3, "heap", "of", "strings");
  975.             
  976.             /* my_string_list =  ("heap", "of", "strings") */
  977.             
  978.             StringListPrepend(my_string_list, "\pA big");
  979.             
  980.             /* my_string_list =  ("A big", "heap", "of", "strings") */
  981.             ...
  982.     notes:
  983.         none.
  984.  
  985.  
  986.  
  987. StringListInsert
  988.  
  989.     formal declaration:
  990.         short StringListInsert(Handle list, StringPtr s);
  991.     
  992.     arguments:
  993.         list = a handle to a string list created by NewStringList,
  994.             MakeStringList, GetStringList, or Get1StringList.
  995.         s = a pointer to the string to add to the list
  996.         
  997.     return value:
  998.         the position in the list of strings where the string was installed,
  999.         or zero if an error occurs.
  1000.             
  1001.     description:
  1002.         StringListInsert adds the string to a string list that is sorted
  1003.         in ascending, non case sensitive, alphabetical order in such a
  1004.         way that the list remains sorted after the string has been added.
  1005.  
  1006.     example application:
  1007.         in this example we insert the string delta into an alphabetically
  1008.         sorted string list.
  1009.     
  1010.     {   Handle my_string_list;
  1011.                         
  1012.             my_string_list = MakeStringList(3, "alpha", "beta", "gamma");
  1013.             
  1014.             /* my_string_list =  ("alpha", "beta", "gamma") */
  1015.             
  1016.             StringListInsert(my_string_list, "\pdelta");
  1017.             
  1018.             /* my_string_list =  ("alpha", "beta", "delta", "gamma") */
  1019.             ...
  1020.     notes:
  1021.         none.
  1022.  
  1023.  
  1024.  
  1025. StringListRInsert
  1026.  
  1027.     formal declaration:
  1028.         short StringListRInsert(Handle list, StringPtr s);
  1029.     
  1030.     arguments:
  1031.         list = a handle to a string list created by NewStringList,
  1032.             MakeStringList, GetStringList, or Get1StringList.
  1033.         s = a pointer to the string to add to the list
  1034.         
  1035.     return value:
  1036.         the position in the list of strings where the string was installed,
  1037.         or zero if an error occurs.
  1038.             
  1039.     description:
  1040.         StringListRInsert adds the string to a string list that is sorted
  1041.         in descending, non case sensitive, alphabetical order in such a
  1042.         way that the list remains sorted after the string has been added.
  1043.  
  1044.     example application:
  1045.         in this example we insert the string delta into an alphabetically
  1046.         sorted string list.  The list is sorted in descending order.
  1047.     
  1048.         {   Handle my_string_list;
  1049.                         
  1050.             my_string_list = MakeStringList(3, "gamma", "beta", "alpha");
  1051.             
  1052.             /* my_string_list =  ("gamma", "beta", "alpha") */
  1053.             
  1054.             StringListRInsert(my_string_list, "\pdelta");
  1055.             
  1056.             /* my_string_list =  ("gamma", "delta", "beta", "alpha") */
  1057.             ...
  1058.     notes:
  1059.         none.
  1060.  
  1061.  
  1062.  
  1063. FindStringList
  1064.  
  1065.     formal declaration:
  1066.         short FindStringList(Handle list, StringPtr s);
  1067.     
  1068.     arguments:
  1069.         list = a handle to a string list created by NewStringList,
  1070.             MakeStringList, GetStringList, or Get1StringList.
  1071.         s = a pointer to the string to find in the list
  1072.         
  1073.     return value:
  1074.         the position in the list of strings where the string was found or zero
  1075.         if the string is not in the list.
  1076.             
  1077.     description:
  1078.         FindStringList searches the list of strings in sequential order until
  1079.         it finds a non case sensitive match for the string.  If a match is
  1080.         the index is returned, otherwise, if there is no match, FindStringList
  1081.         returns zero.
  1082.  
  1083.     example application:
  1084.         Here, we use the FindStringList routine to find the index of the
  1085.         indicated string.
  1086.     
  1087.         {   Handle my_string_list;
  1088.             short position;
  1089.             
  1090.             my_string_list = MakeStringList(5, "the", "word", "is",
  1091.                 "hidden", "here");
  1092.                         
  1093.             position = FindStringList(my_string_list, "\phidden");
  1094.             if (position != 0) {
  1095.                 /* position ==  4 */
  1096.             }
  1097.             
  1098.             position = FindStringList(my_string_list, "\pUnused");
  1099.                 
  1100.                 /* position ==  0, 'unused' is not in the list */
  1101.             ...
  1102.     notes:
  1103.         none.
  1104.  
  1105.  
  1106.  
  1107. StringListToMenu
  1108.  
  1109.     formal declaration:
  1110.         MenuHandle StringListToMenu(Handle list, short id, StringPtr name);
  1111.     
  1112.     arguments:
  1113.         list = a handle to a string list created by NewStringList,
  1114.             MakeStringList, GetStringList, or Get1StringList.
  1115.         id = the menu id for the menu
  1116.         name = the menu title, NULL is OK.
  1117.         
  1118.     return value:
  1119.         the created menu handle.
  1120.             
  1121.     description:
  1122.         StringListToMenu creates a new menu handle filling in the menu
  1123.         items using the strings in the string list.  items in the string list
  1124.         will correspond to items in the menu on a one to one basis.
  1125.         i.e. string list element one will be the same as menu item one,
  1126.         and so on, and so on....
  1127.  
  1128.     example application:
  1129.         Here, we create a menu for use with the PopUpMenuSelect function..
  1130.        
  1131.         {   Handle my_string_list;
  1132.             MenuHandle my_menu;
  1133.             long result;
  1134.             short item;
  1135.             
  1136.             my_string_list = MakeStringList(3, "Red", "Green", "Blue");
  1137.             
  1138.             my_menu = StringListToMenu(my_string_list, 128, "\pColours");
  1139.             
  1140.             InsertMenu(my_menu, -1);
  1141.             result = PopUpMenuSelect(my_menu, 100, 100, 1);
  1142.             DeleteMenu(128);
  1143.             if (HiWord(result) != 0) {
  1144.                 item = LoWord(result);
  1145.                 .....
  1146.             }
  1147.             ...
  1148.     notes:
  1149.         by default, StringListToMenu calls SetItem to add the strings into the
  1150.         menu handle and as such the set of special menu formatting characters
  1151.         are not interpreted by the menu manager.  If you would like to have
  1152.         the menu formatting characters interpreted when string list elements
  1153.         are added to menus then you can define the compile time variable
  1154.         INTERPRETMENUCHARS in the file "strlist.h".
  1155.  
  1156.  
  1157.  
  1158. StringListToList
  1159.  
  1160.     formal declaration:
  1161.         void StringListToList(Handle list, ListHandle the_list);
  1162.     
  1163.     arguments:
  1164.         list = a handle to a string list created by NewStringList,
  1165.             MakeStringList, GetStringList, or Get1StringList.
  1166.         the_list = a list handle
  1167.         
  1168.     return value:
  1169.         none.
  1170.             
  1171.     description:
  1172.         StringListToList fills in the first column of the ListHandle so
  1173.         it contains all of the strings from the string list.
  1174.  
  1175.     example application:
  1176.         here, we copy a string list into a list manager list.
  1177.     
  1178.     {   Handle my_string_list;
  1179.             ListHandle my_list_manager_list;
  1180.             WindowPtr the_window;
  1181.             Rect bounds, dataBounds;
  1182.             Point cSize;
  1183.             
  1184.                 /* set up some stuff */
  1185.             SetPt(&cSize, 0, 0);
  1186.             SetRect(&dataBounds, 0, 0, 1, 0);
  1187.             SetRect(&bounds, 100, 100, 200, 200);
  1188.             
  1189.                 /* make a window */
  1190.             the_window = NewWindow(NULL, &bounds, "\pstrlist",
  1191.                 true, noGrowDocProc, (WindowPtr) (-1), false, 0);
  1192.             SetPort(the_window);
  1193.             
  1194.                 /* make a list */
  1195.             OffsetRect(&bounds, -bounds.left, -bounds.top);
  1196.             my_list_manager_list = LNew(&bounds, &dataBounds, cSize,
  1197.                     0, the_window, true, false, false, true);
  1198.             
  1199.                 /* make a string list */
  1200.             my_string_list = MakeStringList(3, "Red", "Green", "Blue");
  1201.             
  1202.                 /* put the string list into the list */
  1203.             StringListToList(my_string_list, my_list_manager_list);
  1204.             ...
  1205.     notes:
  1206.         recall string lists elements are indexed elt = 1, 2, ..., n while
  1207.         elements contained in ListHandles created by StringListToList are
  1208.         indexed cell.v = 0, 1, 2, ..., n-1.
  1209.  
  1210.  
  1211.  
  1212. StringListUnion
  1213.  
  1214.     formal declaration:
  1215.         Handle StringListUnion(Handle A, Handle B);
  1216.     
  1217.     arguments:
  1218.         A = a handle to a string list created by NewStringList, MakeStringList,
  1219.             GetStringList, or Get1StringList.
  1220.         B = a handle to a string list created by NewStringList, MakeStringList,
  1221.             GetStringList, or Get1StringList.
  1222.         
  1223.     return value:
  1224.         a string list handle.
  1225.             
  1226.     description:
  1227.         StringListUnion returns a new string list handle containing all
  1228.         of the strings that appear in both string list A and string list B,
  1229.         or NULL if an error occurs.  if SLUSECASE is true, case sensitive
  1230.         comparisons are used.  operation = OR
  1231.  
  1232.     example application:
  1233.         here, we calculate the union of two sets of strings.
  1234.     
  1235.         {    Handle A, B, C;
  1236.  
  1237.             A = MakeStringList(4, "red", "green", "orange", "blue");
  1238.             
  1239.             B = MakeStringList(4, "yellow", "green", "purple", "blue");
  1240.             
  1241.             C = StringListUnion(A, B);
  1242.             
  1243.             /* C now contains:
  1244.                 {"blue", "green", "orange", "purple", "red", "yellow"} */
  1245.  
  1246.             ...
  1247.     notes:
  1248.         in the present implementation, the result will be alphabetically
  1249.         sorted.
  1250.  
  1251.  
  1252.  
  1253. StringListIntersection
  1254.  
  1255.     formal declaration:
  1256.         Handle StringListIntersection(Handle A, Handle B);
  1257.     
  1258.     arguments:
  1259.         A = a handle to a string list created by NewStringList, MakeStringList,
  1260.             GetStringList, or Get1StringList.
  1261.         B = a handle to a string list created by NewStringList, MakeStringList,
  1262.             GetStringList, or Get1StringList.
  1263.         
  1264.     return value:
  1265.         a string list handle.
  1266.             
  1267.     description:
  1268.         StringListIntersection returns a new string list handle containing only
  1269.         those strings that appear in both string list A and string list B,
  1270.         or NULL if an error occurs.  if SLUSECASE is true, case sensitive
  1271.         comparisons are used. operation = AND
  1272.  
  1273.     example application:
  1274.         here, we calculate the intersection of two sets of strings.
  1275.     
  1276.         {   Handle A, B, C;
  1277.  
  1278.             A = MakeStringList(4, "red", "green", "orange", "blue");
  1279.             
  1280.             B = MakeStringList(4, "yellow", "green", "purple", "blue");
  1281.             
  1282.             C = StringListIntersection(A, B);
  1283.             
  1284.             /* C now contains:
  1285.                 {"blue", "green"} */
  1286.  
  1287.             ...
  1288.     notes:
  1289.         in the present implementation, the result will be alphabetically
  1290.         sorted.
  1291.  
  1292.  
  1293.  
  1294. StringListDifference
  1295.  
  1296.     formal declaration:
  1297.         Handle StringListDifference(Handle A, Handle B);
  1298.     
  1299.     arguments:
  1300.         A = a handle to a string list created by NewStringList, MakeStringList,
  1301.             GetStringList, or Get1StringList.
  1302.         B = a handle to a string list created by NewStringList, MakeStringList,
  1303.             GetStringList, or Get1StringList.
  1304.         
  1305.     return value:
  1306.         a string list handle.
  1307.             
  1308.     description:
  1309.         StringListDifference returns a new string list handle containing all
  1310.         the strings that do not appear in both A and B.  if SLUSECASE is true,
  1311.         case sensitive comparisons are used.  operation = XOR
  1312.  
  1313.     example application:
  1314.         here, we calculate the difference of two sets of strings.
  1315.     
  1316.         {   Handle A, B, C;
  1317.  
  1318.             A = MakeStringList(4, "red", "green", "orange", "blue");
  1319.             
  1320.             B = MakeStringList(4, "yellow", "green", "purple", "blue");
  1321.             
  1322.             C = StringListDifference(A, B);
  1323.             
  1324.             /* C now contains:
  1325.                 {"orange", "purple", "red", "yellow"} */
  1326.  
  1327.             ...
  1328.     notes:
  1329.         in the present implementation, the result will be alphabetically 
  1330.         sorted.
  1331.  
  1332.  
  1333.  
  1334. StringListSubset
  1335.  
  1336.     formal declaration:
  1337.         Boolean StringListSubset(Handle A, Handle B);
  1338.     
  1339.     arguments:
  1340.         A = a handle to a string list created by NewStringList, MakeStringList,
  1341.             GetStringList, or Get1StringList.
  1342.         B = a handle to a string list created by NewStringList, MakeStringList,
  1343.             GetStringList, or Get1StringList.
  1344.         
  1345.     return value:
  1346.         true or false.
  1347.             
  1348.     description:
  1349.         StringListSubset returns true if the strings contained in B make up
  1350.         a subset of the strings contained in A.  if SLUSECASE is true,
  1351.         case sensitive comparisons are used.
  1352.  
  1353.     example application:
  1354.         here, we call StringListSubset to determine if B is a subset of A.
  1355.     
  1356.         {   Handle A, B, C;
  1357.  
  1358.             A = MakeStringList(4, "red", "green", "orange", "blue");
  1359.             
  1360.             B = MakeStringList(4, "red", "orange", "blue");
  1361.             
  1362.             if (StringListSubset(A, B)) {
  1363.                 /* B is infact a subset of A */
  1364.             }
  1365.  
  1366.             ...
  1367.     notes:
  1368.         none.
  1369.  
  1370.  
  1371.  
  1372.  
  1373. StringListEquivalent
  1374.  
  1375.     formal declaration:
  1376.         Boolean StringListEquivalent(Handle A, Handle B);
  1377.     
  1378.     arguments:
  1379.         A = a handle to a string list created by NewStringList, MakeStringList,
  1380.             GetStringList, or Get1StringList.
  1381.         B = a handle to a string list created by NewStringList, MakeStringList,
  1382.             GetStringList, or Get1StringList.
  1383.         
  1384.     return value:
  1385.         true or false.
  1386.             
  1387.     description:
  1388.         StringListEquivalent returns true if the strings both string lists
  1389.         contain the same strings.  if SLUSECASE is true,  case sensitive
  1390.         comparisons are used.
  1391.  
  1392.     example application:
  1393.         here, we call StringListEquivalent to if the two sets are the same.
  1394.     
  1395.         {    Handle A, B;
  1396.  
  1397.             A = MakeStringList(4, "red", "green", "orange", "blue");
  1398.             
  1399.             B = MakeStringList(4, "red", "green", "orange", "blue");
  1400.             
  1401.             if (StringListEquivalent(A, B)) {
  1402.                 /* yes, the are the same sets */
  1403.             }
  1404.  
  1405.             ...
  1406.     notes:
  1407.         none.
  1408.  
  1409.  
  1410. file strlist.txt Copyright (C) 1995, 1996 by John Montbriand.  All Rights Reserved.
  1411.  
  1412. end of file
  1413.